#include"Queue.h"
void QNodeInit(Queue* pq) {
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}
bool QIsEmpty(Queue* pq) {
	assert(pq);
	return pq->phead == pq->ptail==NULL;
}
QNode* QBuyNode(QDataType val) {
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL) {
		perror("malloc fail");
		exit(1);
	}
	newnode->next = NULL;
	newnode->val = val;
}
void QNodePush(Queue* pq,QDataType val) {
	assert(pq);
	QNode* newnode = QBuyNode(val);
	if (QIsEmpty(pq)) {
		pq->phead = pq->ptail = newnode;
	}
	else {
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}
void QNodePop(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	if (pq->phead == pq->ptail) {
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else {
		QNode* next = pq->phead->next;
		free(pq->phead);
		pq->phead = next;
	}
	pq->size--;
}

QDataType QFront(Queue* pq) {
	assert(pq);
	assert(!QIsEmpt(pq));
	return pq->phead->val;
}
QDataType QBack(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	return pq->ptail->val;
}
int QSize(Queue* pq) {
	assert(pq);
	return pq->size;
}
void QNodeDestroy(Queue* pq) {
	assert(pq);
	assert(!QIsEmpty(pq));
	QNode* pcur = pq->phead;
	QNode* next = pq->phead->next;
	while (pcur) {
		free(pcur);
		pcur = next;
		if (next)
			next = next->next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}